package demo.Socket.Upload_picture;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/20-08-20-11:33
 * @Description：demo.Socket
 */
public class Uploading_server {
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket accept = null;
        InputStream in = null;
        FileOutputStream out = null;
        try {
            server = new ServerSocket(6677);
            System.out.println("等待客户端连接。。。。。。");
            while(true){
                accept = server.accept();
                in = accept.getInputStream();
                byte[] b = new byte[1024];
                int len = -1;
                File file = new File("D:\\qq文件");
                String fileName = "hjy" + System.currentTimeMillis()+ new Random().nextInt(9999);
                out = new FileOutputStream(file+fileName);
                while((len = in.read(b))!= -1){
                    out.write(b,0,len);
                }
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.close();
                }
                if(accept != null){
                    accept.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
